package com.tcoiss.datafactory.controller;

import com.tcoiss.common.core.utils.SecurityUtils;
import com.tcoiss.common.core.utils.poi.ExcelUtil;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.core.web.page.TableDataInfo;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import com.tcoiss.datafactory.domain.OaK3Mapping;
import com.tcoiss.datafactory.service.IOaK3MappingService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * OA同步星空映射Controller
 * 
 * @author zw
 * @date 2021-05-23
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mapping" )
public class OaK3MappingController extends BaseController {

    private final IOaK3MappingService iOaK3MappingService;

    /**
     * 查询OA同步星空映射列表
     */
    @PreAuthorize(hasPermi = "datafactory:mapping:list")
    @GetMapping("/list")
    public TableDataInfo mapTables(OaK3Mapping oaK3Mapping) {
        startPage();
        List<OaK3Mapping> list = iOaK3MappingService.queryList(oaK3Mapping);
        return getDataTable(list);
    }
    /**
     * 导出OA同步星空映射列表
     */
    @PreAuthorize(hasPermi = "datafactory:mapping:export" )
    @Log(title = "OA同步星空映射" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,OaK3Mapping oaK3Mapping) throws IOException
    {
        List<OaK3Mapping> list = iOaK3MappingService.queryList(oaK3Mapping);
        ExcelUtil<OaK3Mapping> util = new ExcelUtil<OaK3Mapping>(OaK3Mapping.class);
        util.exportExcel(response,list, "mapping" );
    }

    /**
     * 获取OA同步星空映射详细信息
     */
    @PreAuthorize(hasPermi = "datafactory:mapping:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iOaK3MappingService.getById(id));
    }

    /**
     * 新增OA同步星空映射
     */
    @PreAuthorize(hasPermi = "datafactory:mapping:add" )
    @Log(title = "OA同步星空映射" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OaK3Mapping oaK3Mapping) {
        oaK3Mapping.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iOaK3MappingService.saveMapping(oaK3Mapping) ? 1 : 0);
    }

    /**
     * 修改OA同步星空映射
     */
    @PreAuthorize(hasPermi = "datafactory:mapping:edit" )
    @Log(title = "OA同步星空映射" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaK3Mapping oaK3Mapping) {
        return toAjax(iOaK3MappingService.updateMappingById(oaK3Mapping) ? 1 : 0);
    }

    /**
     * 删除OA同步星空映射
     */
    @PreAuthorize(hasPermi = "datafactory:mapping:remove" )
    @Log(title = "OA同步星空映射" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}" )
    public AjaxResult remove(@PathVariable Long[] id) {
        return toAjax(iOaK3MappingService.removeByIds(Arrays.asList(id)) ? 1 : 0);
    }

    /**
     * 根据映射关系将OA数据同步至星空生成单据
     */
    @Log(title = "OA同步星空映射" , businessType = BusinessType.INSERT)
    @PostMapping("/syncK3BillByMapping")
    public AjaxResult syncK3BillByMapping(@RequestBody Map<String,Object> oaMap) {
        return toAjax(iOaK3MappingService.syncK3BillByMapping(oaMap) ? 1 : 0);
    }

    /**
     *  更新使用状态
     */
    @PreAuthorize(hasPermi = "datafactory:mapping:edit")
    @GetMapping(value = "/updateUseStatus")
    public AjaxResult updateUseStatus(Long[] workIds, Integer status){
        return AjaxResult.success(iOaK3MappingService.updateUseStatus(Arrays.asList(workIds),status) ? 1 : 0);
    }
}
